package com.ycrlkj.android.modulescore.main.activity;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.ycrlkj.android.modulescore.main.databinding.ActivityFreightOrderTabsBinding;
import com.ycrlkj.android.modulespublic.common.constant.RoutePath;
import com.ycrlkj.android.modulespublic.common.widget.titlebar.MTitleBar;

import java.util.ArrayList;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
@Route(path = RoutePath.PAGE_FREIGHT_ORDER_TABS)
public class FreightOrderTabsActivity extends AppCompatActivity {

    private ActivityFreightOrderTabsBinding binding;

    private ViewPager2 viewPager;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityFreightOrderTabsBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        initTitleBar();
        initTabs();
    }

    public void initTabs() {
        viewPager = binding.viewPager;
        tabLayout = binding.tabLayout;

        ArrayList<String> titles = new ArrayList<>();
        titles.add("已接单");
        titles.add("已完成");
        titles.add("已取消");

        ArrayList<FreightOrderListFragment> fragments = new ArrayList<>();
        fragments.add(FreightOrderListFragment.newInstance(2));
        fragments.add(FreightOrderListFragment.newInstance(3));
        fragments.add(FreightOrderListFragment.newInstance(4));

        viewPager.setAdapter(new FragmentStateAdapter(this) {
            @Override
            public int getItemCount() {
                return fragments.size();
            }

            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }
        });

        new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles.get(position));
            }
        }).attach();
    }

    public void initTitleBar() {
        MTitleBar titleBar = binding.mTitleBar;
        titleBar.setBtnEvents(new MTitleBar.BtnEvents() {
            @Override
            public void setBtnLeftOnClickListener() {
                FreightOrderTabsActivity.this.finish();
            }

            @Override
            public void setBtnRightOnClickListener() {
            }
        });
    }

}